<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Web\Controllers;

use App\Api\Account\Models\UserAccountTransaction;
use App\Api\Account\Services\BusinessService;
use App\Api\Auth\Models\Users;
use App\Api\Auth\Models\UsersOpenId;
use App\Api\Web\Models\Comment;
use App\Api\Web\Models\CommentReply;
use App\Http\Controllers\Controller;
use App\Libs\Wechat\WechatService;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CommentController extends Controller
{
    public function comment(Request $request, $source)
    {
        $params = $request->all();
        $user = authUser();
        $postId = data_get($params, 'post_id', '');
        $text = data_get($params, 'text', '');
        if (empty($postId)) {
            throw new \LogicException('请先选择房源', 100);
        }

        if (empty($text)) {
            throw new \LogicException('请输入评论的内容', 110);
        }

        $appId = config('payment.jueqiweilaixcx.wechat.app_id');
        $secret = '';
        if ($appId == config('payment.jueqiweilaixcx.wechat.app_id')) {
            $secret = config('payment.jueqiweilaixcx.wechat.app_secret');
        }
        $wechat  = new WechatService($appId, $secret);
        $r = $wechat->msgSecCheck($text);

        switch ($source) {
            case 'house':
                $type = 1;
                $r = \DB::table('house')->where('id', $postId)->increment('comment_count');
                break;
            case 'house-wanted':
                $type = 2;
                $r = \DB::table('house_wanted')->where('id', $postId)->increment('comment_count');
                break;
            case 'bbs':
                $type = 3;
                $r = \DB::table('bbs')->where('id', $postId)->increment('comment_count');
                break;
            case 'house-bbs':
                $type = 4;
                $r = \DB::table('house')->where('id', $postId)->increment('comment_count');
                break;
            case 'house-wanted-bbs':
                $type = 5;
                $r = \DB::table('house_wanted')->where('id', $postId)->increment('comment_count');
                break;
            default:
                throw new NotFoundHttpException();
        }


        $r = Comment::create([
            'type' => $type,
            'post_id' => $postId,
            'user_id' => $user->id,
            'text' => $text,
        ]);

        $isExist = UserAccountTransaction::where('user_id', '=', $user->id)
            ->where('type', '=', '3')
            ->where('source_id', '=', data_get($r, 'id'))
            ->where('source_type', '=', 'comment')
            ->first();
        if (!empty($isExist)) {
            BusinessService::commonIntegralTransaction($user->id, 5,  data_get($r, 'id'), 'comment', '评论送积分');
        }


        return $this->formatOutput($r);
    }

    public function commentReply(Request $request)
    {
        $params = $request->all();
        $toUserId = data_get($params, 'to_user_id', 0);
        $commentId = data_get($params, 'comment_id', 0);
        $user = authUser();
        $text = data_get($params, 'text', '');

        if (empty($commentId)) {
            throw new \LogicException('请重新选择评论', 100);
        }

        if (empty($text)) {
            throw new \LogicException('请输入评论的内容', 110);
        }
        $appId = config('payment.jueqiweilaixcx.wechat.app_id');
        $secret = '';
        if ($appId == config('payment.jueqiweilaixcx.wechat.app_id')) {
            $secret = config('payment.jueqiweilaixcx.wechat.app_secret');
        }
        $wechat  = new WechatService($appId, $secret);
        $r = $wechat->msgSecCheck($text);
        $r = CommentReply::create([
            'comment_id' => $commentId,
            'text' => $text,
            'from_user_id' => $user->id,
            'to_user_id' => $toUserId,
        ]);

        $isExist = UserAccountTransaction::where('user_id', '=', $user->id)
            ->where('type', '=', '3')
            ->where('source_id', '=', data_get($r, 'id'))
            ->where('source_type', '=', 'comment_reply')
            ->first();
        if (!empty($isExist)) {
            BusinessService::commonIntegralTransaction($user->id, 5,  data_get($r, 'id'), 'comment_reply', '回复送积分');
        }

        $comment = Comment::find($commentId);

        $type = data_get($comment, 'type', '');
        $postId = data_get($comment, 'post_id', '');
        $r = \DB::table('comment')->where('id', $postId)->increment('reply_count');
        switch ($type) {
            case '1':
                $type = 1;
                $r = \DB::table('house')->where('id', $postId)->increment('comment_count');
                break;
            case '2':
                $type = 2;
                $r = \DB::table('house_wanted')->where('id', $postId)->increment('comment_count');
                break;
            case '3':
                $type = 3;
                $r = \DB::table('bbs')->where('id', $postId)->increment('comment_count');
                break;
            case '4':
                $type = 4;
                $r = \DB::table('house')->where('id', $postId)->increment('comment_count');
                break;
            case '5':
                $type = 5;
                $r = \DB::table('house_wanted')->where('id', $postId)->increment('comment_count');
                break;
            default:
        }


        return $this->formatOutput('success');
    }

    public function getComment(Request $request, $source)
    {
        $params = $request->all();
        $postId = data_get($params, 'post_id', '');
        $page = data_get($params, 'page', '');
        $pageSize = data_get($params, 'page_size', '');
        if (empty($postId)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        switch ($source) {
            case 'house':
                $type = 1;
                break;
            case 'house-wanted':
                $type = 2;
                break;
            case 'bbs':
                $type = 3;
                break;
            case 'house-bbs':
                $type = 4;
                break;
            case 'house-wanted-bbs':
                $type = 5;
                break;
            default:
                throw new NotFoundHttpException();
        }

        $r = Comment::where('type', '=', $type)
            ->leftJoin('users as u', 'u.id', '=', 'comment.user_id')
            ->select('comment.*', 'u.head_img', 'u.name')
            ->where('post_id', '=', $postId)
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        return $this->formatOutput($r);
    }

    public function getCommentReply(Request $request)
    {
        $params = $request->all();
        $commentId = data_get($params, 'comment_id', '');
        if (empty($commentId)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        $page = data_get($params, 'page', '');
        $pageSize = data_get($params, 'page_size', '');

        $r = CommentReply::where('comment_id', '=', $commentId)
            ->leftJoin('users as u1', 'u1.id', '=', 'comment_reply.from_user_id')
            ->leftJoin('users as u2', 'u2.id', '=', 'comment_reply.to_user_id')
            ->select('comment_reply.*', 'u1.head_img as from_user_head_img', 'u2.head_img as to_user_head_img',
                'u1.name as from_user_name', 'u2.name as to_user_name')
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        $replyCount = data_get($r, 'total', 0);
        Comment::find($commentId)->update([
            'reply_count' => $replyCount
        ]);

        return $this->formatOutput($r);
    }

    public function adminGetComment(Request $request)
    {
        $params = $request->all();
    }
}
